﻿using System;
using viPage.Classes;
using viPage.Controllers;
using visinia.SmartEngine;

public partial class Admin_Pages_viPage_Page : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {        
        if (Request.QueryString["PageId"] != null)
        {
            int pageid = 0;
            if (Int32.TryParse(Request.QueryString["PageId"], out pageid))
            {
                viPageInfo page = viPageController.GetPage(pageid);
                if (page != null)
                {
                    PAGE_NAME.Text = page.Name;
                    PAGE_TITLE.Text = page.Title;
                }
            }
        }
    }

    protected void SAVE_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["PageId"] == null)
        {//create a new page
            viPageInfo page = new viPageInfo();
            page.Name = PAGE_NAME.Text;
            page.Title = PAGE_TITLE.Text;
            int pageid = viPageController.AddPage(page);
            // reload the newly created page
            Response.Redirect(SystemMenu.CallPage("viPage", "EditPage", "PageId", pageid.ToString()) + "&saved=true");            
        }
        else
        {// edit an already existing page
            int pageid = 0;
            if (Int32.TryParse(Request.QueryString["PageId"], out pageid))
            {
                viPageInfo page = viPageController.GetPage(pageid);
                if (page != null)
                {
                    page.Name = PAGE_NAME.Text;
                    page.Title = PAGE_TITLE.Text;
                    viPageController.UpdatePage(page);
                }
            }
        }
        Response.Redirect(Request.RawUrl);
    }

    protected void CANCEL_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.RawUrl);
    }
}